package com.oa.common.handler;

import com.oa.common.config.exception.OAException;
import com.oa.common.result.Result;
import com.oa.common.result.ResultCodeEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.nio.file.AccessDeniedException;


/**
 * @author Admin
 * @title: GlobalExceptionHandler
 * @projectName Cloud_OA_Office
 * @description: GlobalExceptionHandler
 * @date 2024/10/2 15:53
 */

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        //return Result.fail().message("执行全局异常处理");
        return Result.fail();
    }

    /**
     * 特定异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(OAException.class)
    @ResponseBody
    public Result error(OAException e){
        e.printStackTrace();
        return Result.fail().code(e.getCode()).message(e.getMsg());
    }

    /**
     * spring security异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.build(null, ResultCodeEnum.PERMISSION);
    }
}



